<?php
/**
 * 企业名片后台管理类
 * User: zal
 * Date: 2016/4/15
 * Time: 14:25
 */
namespace Admin\Controller;

class CompanyController extends CommonController{

    protected function _initialize(){
        parent::_initialize();
        $this->breadcrumb1='系统';
        $this->breadcrumb2='企业名片';
    }

    public function index(){

        $companyModel=M('Company');
        $keyword=I('get.keyword','');
        $scid=I('get.scid','');
        $status=I('get.status','');
        $map=array();
        $sql="select co.id,co.company_name,co.addrss,co.tel,co.icon,co.lng,co.lat,co.addtime,sc.name from ".C('DB_PREFIX')."company as co
                left join ".C('DB_PREFIX')."shop_category as sc on co.sc_id=sc.sc_id ";
        $sql.="where 1=1 ";
        if(!empty($keyword)){
            $sql.="and co.company_name like '%$keyword%' ";
        }
        if(!empty($scid)){
            $sql.="and co.sc_id = $scid ";
        }
        if(!empty($status)){
            $sql.="and co.status = $status ";
        }

        $count=count($companyModel->query($sql));
        $Page = new \Think\Page($count,C('BACK_PAGE_NUM'));

        $show  = $Page->show();// 分页显示输出

        $sql.="order by id desc LIMIT ".$Page->firstRow.",".$Page->listRows;
        $list=$companyModel->query($sql);
        $Page = new \Think\Page($count,C('BACK_PAGE_NUM'));

        $show  = $Page->show();// 分页显示输出
        $this->category=M('shop_category')->select();
        $this->assign('empty','<tr><td colspan="6">~~暂无数据</td></tr>');// 赋值数据集
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('status',$status);// 赋值分页输出

        $this->display();
    }

    function add(){
        if(IS_POST){
            $icon = I('post.icon','');
            $sc_id = I('post.sc_id','');
            $company_name = I('post.company_name','');
            $desc = I('post.desc','');
            $remark = I('post.remark','');
            $address = I('post.address','');
            $ad = I('post.ad','');
            $tel = I('post.tel','');
            $status = I('post.status','');

            $data['icon'] = $icon;
            $data['sc_id'] = $sc_id;
            $data['company_name'] = $company_name;
            $data['desc'] = $desc;
            $data['remark'] = $remark;
            $data['address'] = $address;
            $data['ad'] = $ad;
            $data['tel'] = $tel;
            $data['status'] = $status;

            $error=$this->validate($data);

            if(!empty($error)){
                $this->ajaxReturn($error,'JSON');
            }

            $r=M('Company')->add($data);
            if($r){
                $this->success('新增成功',U('Company/index'));
            }else{
                $this->error('新增失败');
            }
        }

        $this->crumbs='新增';
        $this->action=U('Company/add');
        $this->display();
    }


    function edit(){

        $id=I('id');

        if(IS_POST){
            $data = array();
            $id = intval($_POST["id"]);
            $payment_config	= '';
            $bank_name = $_POST["bank_name"];//配置参数

            $error=$this->validate($bank_name);

            if(!empty($error)){
                $this->error($error["message"]);
            }

            $status = $_POST["status"];//配置参数
            $data['bank_name'] = $bank_name;
            $data['status'] = $status;
            $r=M('BankList')->where(array('bank_id'=>$id))->save($data);
            //echo M('BankList')->getLastSql();exit;
            if($r){
                $this->success('编辑成功',U('BankList/index'));
            }else{
                $this->error('编辑失败');
            }
            die;
        }
        $this->action=U('BankList/edit');

        $bank = M('BankList')->where(array('bank_id'=>$id))->find();

        $this->bank_array=array(
            'bank_id'=>$bank['bank_id'],
            'bank_name'=>$bank['bank_name'],
            'status'=>$bank['status']
        );
        $this->crumbs="编辑";

        $this->display();
    }

    private function validate($data){

        $error=array();
        if(empty($data["company_name"])){
            $error["msg"]='企业名称必填';
        }else if(empty($data["sc_id"])){
            $error["msg"]='店铺分类必填';
        }else if(empty($data["icon"])){
            $error["msg"]='店铺图片必填';
        }else if(empty($data["desc"])){
            $error["msg"]='店铺描述必填';
        }else if(empty($data["remark"])){
            $error["msg"]='店铺备注必填';
        }else if(empty($data["address"])){
            $error["msg"]='店铺地址必填';
        }else if(empty($data["tel"])){
            $error["msg"]='联系电话必填';
        }

        if(!empty($error)){
            return array(
                'status'=>'500',
                'message'=>$error["msg"]
            );
        }else{
            return array(
                'status'=>'200',
                'message'=>"ok"
            );
        }
    }



}